﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using RA.UI.Messages;
using RA.CompileServices;
using RA.Common;
using Microsoft.Win32;

namespace RA.UI.ViewModel
{
    public class AgentsManagerViewModel : ViewModelBase
    {
        CompileService _compileService;
        string _defaultPath;
        string _defaultGrabberPath;

        public AgentsManagerViewModel(CompileService compileService)
        {
            _compileService = compileService;
            _defaultPath = AppDomain.CurrentDomain.BaseDirectory + "Agents";
            _defaultGrabberPath = AppDomain.CurrentDomain.BaseDirectory + "TextAgents";
        }

        string _keys;
        public string Keys
        {
            get { return _keys; }

            set
            { 
                if(_keys != value)
                {
                    _keys = value;
                    RaisePropertyChanged(() => Keys);
                }
            }
        }

        string _urlTemplate;
        public string URLTemplate
        {
            get { return _urlTemplate; }

            set
            { 
                if(_urlTemplate != value)
                {
                    _urlTemplate = value;
                    RaisePropertyChanged(() => URLTemplate);
                }
            }
        }

        string _bodyXPath;
        public string BodyXPath
        {
            get { return _bodyXPath; }

            set
            { 
                if(_bodyXPath != value)
                {
                    _bodyXPath = value;
                    RaisePropertyChanged(() => BodyXPath);
                }
            }
        }

        List<KeyValuePair<string, int>> _results;
        public List<KeyValuePair<string, int>> Results
        {
            get { return _results; }

            set
            {
                if (_results != value)
                {
                    _results = value;
                    RaisePropertyChanged(() => Results);
                }
            }
        }

        ICommand _generateAgent;
        public ICommand GenerateAgent
        {
            get
            {
                if(_generateAgent == null)
                    _generateAgent = new RelayCommand<string>((param) =>
                {
                    _compileService.CompileImageRecognitionAgent(Settings.Current, param);
                });

                return _generateAgent;
            }
        }

        ICommand _changeAgent;
        public ICommand ChangeAgent
        {
            get
            {
                if (_changeAgent == null)
                    _changeAgent = new RelayCommand<string>((param) =>
                    {
                        OpenFileDialog();
                       // Messenger.Default.Send(new ChangeAgentMessage(_compileService.GetAgent(param))); 
                    });

                return _changeAgent;
            }
        }


        ICommand _generateTextAgentCommand;
        public ICommand GenerateTextAgentCommand
        {
            get
            {
                if (_generateTextAgentCommand == null)
                    _generateTextAgentCommand = new RelayCommand<string>((param) =>
                    {
                        _compileService.CompileTextFinderAgent(URLTemplate, BodyXPath, Keys.Split(' '), param); 
                    });

                return _generateTextAgentCommand;
            }
        }

        ICommand _openCommand;
        public ICommand OpenCommand
        {
            get
            {
                if (_openCommand == null)
                    _openCommand = new RelayCommand<string>((param) =>
                    {
                        OpenFileGrabberDialog();
                    });

                return _openCommand;
            }
        }

        void OpenFileGrabberDialog()
        {
            var dialog = new OpenFileDialog { InitialDirectory = _defaultGrabberPath };
            dialog.Filter = "Файлы шаблонов (*.dll)|*.dll";
            if (dialog.ShowDialog() == true)
            {
                var agent = _compileService.GetGrabberAgent(dialog.FileName);
                Results = agent.GetResults().ToList();
            }
        }


        void OpenFileDialog()
        {
            var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
            dialog.Filter = "Файлы шаблонов (*.dll)|*.dll";
            if (dialog.ShowDialog() == true)
            {
                Messenger.Default.Send(new ChangeAgentMessage(_compileService.GetAgent(dialog.FileName)));
            }
        }
    }
}
